package com.example.controller;

import com.example.entity.Employee;
import com.example.service.EmployeeService;
import com.example.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/emp")
@CrossOrigin
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    
    @GetMapping("/list")
    public Result list(String empName, Integer deptId, 
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Employee> list = employeeService.getList(empName, deptId);
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return Result.success();
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return Result.success();
    }
    
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        employeeService.delete(id);
        return Result.success();
    }
} 